package com.cookman.gameobjects.decks;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.cookman.cards.CityCard;
import com.cookman.gameobjects.CityCardActor;
import com.cookman.gameobjects.controls.ModalPopup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cookman on 12.09.14.
 */
public class CardDeckActor extends ModalPopup {

    private List<CityCardActor> cardActors;

    public CardDeckActor() {
        super();
    }

    private float padding = 0.2f;
    protected float scale = 1.2f;

    private boolean isMultiSelect = false;
    private int maxSelected = 5;

    public boolean isMultiSelect() {
        return isMultiSelect;
    }

    public void setMultiSelect(boolean isMultiSelect) {
        this.isMultiSelect = isMultiSelect;
    }

    public int getMaxSelected() {
        return maxSelected;
    }

    public void setMaxSelected(int maxSelected) {
        this.maxSelected = maxSelected;
    }

    public void setPlayerCards(List<CityCard> playerCards) {
        float startX = 0.2f;
        float startY = getHeight() - scale;
        clearChildren();
        addAcceptCancel();
        cardActors = new ArrayList<>();

        if (playerCards.size() > 3) {
            scale = 0.8f;
        }

        int count = 0;
        if (playerCards != null)
            for (CityCard card : playerCards) {
                count += 1;
                if (count > 4) {
                    startY -= scale + padding;
                    startX = 0.2f;
                    count = 1;
                }
                CityCardActor actor = new CityCardActor(card, this);
                actor.setX(startX);
                actor.setScale(scale);
                actor.setBounds(startX, startY, scale, scale * (actor.getHeight() / actor.getWidth()));
                startX += padding + actor.getWidth();
                addActor(actor);
                cardActors.add(actor);
            }
    }

    public void resetSelection() {
        if (isMultiSelect) return;
        if (cardActors != null) {
            for (CityCardActor cardActor : cardActors) {
                cardActor.setIsSelected(false);
            }
        }
    }

    public List<CityCard> getSelectedCityCards(){
        List<CityCard> result=new ArrayList<>();
        for(CityCardActor actor:cardActors){
            if(actor.getIsSelected()){
                result.add(actor.getCard());
            }
        }
        return result;
    }

    @Override
    public void renderShape(ShapeRenderer shapeRenderer) {
        if (cardActors != null)
            for (CityCardActor cardActor : cardActors) {
                cardActor.renderShape(shapeRenderer);
            }
    }
}
